﻿//创建时间: 2010-12-15
//创建作者: 李 城
//功能说明: cms外部页面访问基类 评论提交
//最后修改: 2010-12-15
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Web;

using TRPOP.Config;
using TRPOP.Common;

namespace TRPOP.CMS.Web.ExtraBus
{
    /// <summary>
    /// 评论提交
    /// </summary>
    public class Page_Load_RecommentSubmit : BasePage
    {

        protected override void OnInit(EventArgs e)
        {
            string strRecommentSubmitUrl = SYSConfig.RecommentSubmitUrl;
            string strDynamicPageName = 
                (base.mCurrentUrl.ToLower().Contains(strRecommentSubmitUrl.ToLower().Trim())) ? "RecommentSubmitUrl" : "";
            if (strDynamicPageName == "RecommentSubmitUrl")
                Rec_Submit();
            else
                Base_Config.NotFound(base.Page, "CMS后台配置的页面地址和访问的页面不一致(评论提交)");
            //base.OnInit(e);
        }
        
        /// <summary>
        /// 评论提交
        /// </summary>
        public void Rec_Submit()
        {
            //验证码验证
            if (SYSConfig.RecommentEnable == false)
                Base_Config.NotFound(base.Page, "CMS后台配置的页面地址和访问的页面不一致(评论未启用)");
            string strName = RequestHelper.RequestString(SYSConfig.RecommentrecName);
            string strDetail = HttpUtility.HtmlEncode( RequestHelper.RequestString(SYSConfig.RecommentrecDetail));
            int intIndexID = RequestHelper.RequestInt(SYSConfig.Recommentindexid);
            string strCheckCode = RequestHelper.RequestString(SYSConfig.RecommentrecvalidCode).ToLower().Trim();
            if (CheckDrawImageCookie("RecommentCookie", strCheckCode) == false)
            {
                Response.Write(
                    string.Format("<script>alert('{0}');window.location.href='{1}';</script>", "验证码不正确", RequestHelper.GetUrlReferrer())
                );
                return;
            }
            RecomManage oRecomManage = new RecomManage();
            RecomContentManage oRecomContentManage = new RecomContentManage();
            RecomInfo oRecomInfo = new RecomInfo();
            ContentInfo oContentInfo = new ContentInfo();
            oContentInfo = new ContentManage().GetInfoById(intIndexID);
            if (oContentInfo == null)
                Base_Config.NotFound(base.Page, "内容信息不存在");
            strName = strName.SQLFilter();
            strDetail = strDetail.SQLFilter() ;
            oRecomInfo.Detail = strDetail;
            oRecomInfo.Name = strName;
            if (oRecomManage.Save(oRecomInfo) == EnumSubmitResult.Success)
            {

                RecomContentInfo oRecomContentInfo = new RecomContentInfo();
                oRecomContentInfo.AddTime = DateTime.Now;
                oRecomContentInfo.AddUserId = 0;
                oRecomContentInfo.ContentId = oContentInfo.ContentId;
                oRecomContentInfo.IP = RequestHelper.GetIP();
                oRecomContentInfo.IsAnonymity = true;
                oRecomContentInfo.IsAudit = !SYSConfig.RecommentAudit;
                oRecomContentInfo.RecomId = oRecomInfo.RecommendId;
                oRecomContentInfo.UserName = oRecomInfo.Name.IsEmpty() ? "匿名" : oRecomInfo.Name;
                oRecomContentManage.Save(oRecomContentInfo);
                Response.Redirect(SYSConfig.RecommentListUrl.ToLower().Replace("{indexid}", oContentInfo.ContentId.ToString())
                                                                      .Replace("{page}", "1")
                                  );
            }
            else
                Base_Config.NotFound(base.Page, "评论提交错误");

        }

        /// <summary>
        /// 验证Cookie
        /// </summary>
        /// <param name="CookieName">Cookie名</param>
        /// <param name="strInput">提交的信息</param>
        /// <returns></returns>
        public bool CheckDrawImageCookie(string CookieName, string strInput)
        {
            bool result = false;

            HttpCookie cookie = Request.Cookies.Get(CookieName);
            if (cookie != null)
            {
                if (cookie.Value.ToLower() == strInput.ToLower())
                {
                    result = true;
                }

            }
            return result;
        }
        
    }
}
